﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

using System;
using System.Collections.Generic;

using Twipler.Data.Entity.Twipler;

using NHibernate.Criterion;
using Twipler.Data.Access;

namespace Twipler.Data.Entity.Theme
{
    public enum StyleState
    {
        Deleted = 0,
        Published = 1,
        Private = 2
    }

    public class Style: Base 
    {
        public Style() : base()
        {
            State = StyleState.Private;
            CreatedBy = App.Current.Visitor;           
        }

        [EditOrder(1)]
        public virtual string Name { get; set; }
                
        [EditOrder(2)]
        [EditCondition(EditCondition.Condition.IsSuperAdmin)]
        public virtual StyleState State { get; set; }

        [EditOrder(3)]
        [EditCondition(EditCondition.Condition.IsSuperAdmin)]
        public virtual Design ParentDesign { get; set; }

        [EditOrder(4)]
        [EditReadonly]
        public virtual Visitor CreatedBy { get; set; }

        [EditOrder(5)]
        [EditIf("UseCanModify_AllowCopy")]
        public virtual bool AllowCopy { get; set; }

        [EditOrder(6)]
        [EditReadonly]
        public virtual Style CopiedStyle { get; set; }

        [EditOrder(7)]
        [EditStringType(EditStringType.Types.LargeText)]
        public virtual string Content { get; set; }

        [EditIgnore]
        public virtual int UseCount
        {
            get
            {
                return (int)Factory.Visitor.Criteria(Restrictions.Eq("Style", this))
                                .SetProjection(Projections.Count("Id"))
                                .UniqueResult();
            }
        }

        public override bool UserCanCreate()
        {
            return App.Current.Visitor.HasRole(VisitorRole.StyleCreator);
        }

        public override bool UserCanModify()
        {
            return App.Current.Visitor.HasRole(VisitorRole.ThemeModerator) ||
                  (CreatedBy != null && CreatedBy.Id == App.Current.Visitor.Id);
        }

        public virtual bool UserCanModify_AllowCopy()
        {
            return (this.CopiedStyle == null);
        }
                
        public virtual List<Style> PublishedAllowCopy()
        {
            return Data.Access.Factory.Style.GetByCriteria(
                Restrictions.And(
                    Restrictions.Eq("AllowCopy", true),
                    Restrictions.Eq("State", (int)StyleState.Published)));
        }

        public override string ToString()
        {
            return Name;
        }

        public virtual Style Clone()
        {
            return new Style()
            {
                Id = 0,
                Name = "Copy of " + this.Name,
                AllowCopy = this.AllowCopy,
                Content = this.Content,
                CopiedStyle = this,
                ParentDesign = this.ParentDesign
            };
        }
    }
}
